#pragma once

#include "CrossDefine.h"
#include <string>
#include "common.h"
using namespace std;

//////////////////////////////////////////////////////////////////////
struct true_type {};
struct false_type {};

#ifdef LD_LINUX
inline char* ltoa(long _Val, char * _DstBuf, int _Radix)
{
    sprintf(_DstBuf, "%ld", _Val);
    return _DstBuf;
}
inline char* ultoa(unsigned long _Val, char * _DstBuf, int _Radix)
{
    sprintf(_DstBuf, "%lu", _Val);
    return _DstBuf;
}
#endif
//////////////////////////////////////////////////////////////////////
const int		STRING_FORMAT_SIZE				= 4096;
class CStr
{
public:
	CStr					(void)							{ m_string = std::string(); }
	CStr					(const char* str)				{ IF_OK(str) m_string = str; }
	CStr&		operator=	(const char* str)				{ IF_OK(str) m_string = str; else m_string = ""; return *this; }
	operator const char*	(void) const					{ return m_string.c_str(); }
protected:
	CStr					(std::string& s)						{ m_string = s; }

public:

	bool	operator<		(const CStr& rhs) const			{ return m_string < rhs.m_string; }
	bool	operator==		(const CStr& rhs) const			{ return m_string == rhs.m_string; }
	CStr&	operator+=		(const CStr& rhs)				{ m_string += rhs.m_string; return *this; }
	CStr&	operator+=		(char ch) 						{ m_string += ch; return *this; }
	CStr	operator+		(const CStr& rhs) const			{ std::string ret = m_string + rhs.m_string; return CStr(ret);}
	int		int_string		(void)							{ return atol(m_string.c_str()); }
	void	format			(const char *format, va_list argptr)	{ CHECK(format); char buf[STRING_FORMAT_SIZE]; 
																	try{ ASSERT(vsprintf(buf, format, argptr) < STRING_FORMAT_SIZE); 
																	}catch(...){ ::log_save("CStr::format"); } m_string=buf; }
	// 作废
	const CStr&	format_long(long n_num)                    { char buf[]="-2147483648"; m_string = ltoa(n_num, buf, 10); return *this; }
	const CStr&	format_dword(unsigned long n_num)			{ char buf[]="-2147483648"; m_string = ultoa(n_num, buf, 10); return *this; }
	string&	query_string	(void)							{ return m_string; }


protected:
	std::string		m_string;
};

//
////////////////////////////////////////////////////////////////////////
//template<TYPENAME T>
//struct dump_traits { static CStr dump(const T& obj)		{ return "?"; } };
//template<>
//struct dump_traits<char> { static CStr dump(const T& obj)		{ return CStr().format_long(obj); } };
//template<>
//struct dump_traits<short> { static CStr dump(const T& obj)	{ return CStr().format_long(obj); } };
//template<>
//struct dump_traits<int> { static CStr dump(const T& obj)		{ return CStr().format_long(obj); } };
//template<>
//struct dump_traits<long> { static CStr dump(const T& obj)		{ return CStr().format_long(obj); } };
//template<>
//struct dump_traits<unsigned char> { static CStr dump(const T& obj)	{ return CStr().format_long(obj); } };
//template<>
//struct dump_traits<unsigned short> { static CStr dump(const T& obj)	{ return CStr().format_long(obj); } };
//template<>
//struct dump_traits<unsigned int> { static CStr dump(const T& obj)		{ return CStr().format_long(obj); } };
//template<>
//struct dump_traits<unsigned long> { static CStr dump(const T& obj)	{ return CStr().format_long(obj); } };
//template<>
//struct dump_traits<char*> { static CStr dump(const T& obj)		{ return CStr(obj); } };
//template<>
//struct dump_traits<const char*> { static CStr dump(const T& obj)	{ return CStr(obj); } };
/*
template<>
struct dump_traits<XXX> { static CStr dump(const T& obj) { return obj.dump(); } };
template<TYPENAME T>
inline	CStr	dump_string(const T& obj)	{ return dump_traits<T>::dump(obj); }
*/

//-------------------------------------
typedef	const CStr&	STRINGREF;
//template<>
//struct dump_traits<CStr> { static CStr dump(const T& obj) { return obj; } };


//////////////////////////////////////////////////////////////////////
template<TYPENAME T>
class POINT_NO_RELEASE : public T
{
	virtual void	release(void)				= 0;		// prevent call this function
	virtual void	release_by_owner(void)		= 0;		// prevent call this function
};

//////////////////////////////////////////////////////////////////////
template<int I>
struct auto_long_
{
	auto_long_() : hide_data(I) {}
	auto_long_<I>& operator =(long n)	{ hide_data=n; return *this; }
	operator long() const	{ return hide_data; }

	long	hide_data;
};

typedef	auto_long_<0>	auto_long;

//////////////////////////////////////////////////////////////////////
template<TYPENAME T>
class hide_type
{
	T	m_hide_data;
public:
	T&	hide_data(void)		{ return m_hide_data; }
};


